package com.cn.pet.service.impl;

import com.cn.pet.mapper.KeywordMapper;
import com.cn.pet.model.pojo.Keyword;
import com.cn.pet.service.KeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public List<Keyword> findAllKeywords() {
        return keywordMapper.queryKeywordByCondition(new HashMap<>());
    }

    @Override
    public List<Keyword> findKeywordByCondition(Map<String,Object> paramMap) {
        return keywordMapper.queryKeywordByCondition(paramMap);
    }

    @Override
    public String addOrUpdateKeyword(Keyword keyword) {
        String result = null;
        if(keyword.getId() == null) {
            try{
                keywordMapper.insert(keyword);
                result = "新增关键字成功！";
            } catch (Exception e) {
                e.printStackTrace();
                result = "新增关键字失败！";
            }
        } else {
            try{
                keywordMapper.update(keyword);
                result = "修改关键字成功！";
            } catch (Exception e) {
                e.printStackTrace();
                result = "修改关键字失败！";
            }
        }
        return result;
    }

    @Override
    public String deleteKeyword(int id) {
        String result = null;
        try{
            keywordMapper.delete(id);
            result = "删除关键字成功！";
        } catch (Exception e) {
            e.printStackTrace();
            result = "删除关键字失败！";
        }
        return result;
    }

}
